package com.platform.core.web.controller;

import cn.hutool.core.collection.CollUtil;
import com.platform.core.service.SysConfTypeService;
import com.platform.dto.ConfTypeQo;
import com.platform.dto.ConfTypeTreeNode;
import com.platform.dto.CrClassifyDto;
import com.platform.dto.SysConfTypeDto;
import com.platform.utils.SysConfUtil;
import com.querydsl.core.BooleanBuilder;
import com.platform.comm.dto.KeyValue;
import com.platform.comm.web.results.DataJsonResult;
import com.platform.comm.web.results.JsonResult;
import com.platform.comm.web.results.PageListJsonResult;
import com.platform.comm.web.results.TreeNode;
import org.apache.commons.collections4.MapUtils;
import org.apache.logging.log4j.util.Strings;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * Description: 类别管理
 *
 * @author libin
 * Created on 2021/7/6
 **/
@RestController
@RequestMapping("management/confType")
public class SysConfTypeController extends BaseController {

    private final SysConfTypeService sysConfTypeService;

    public SysConfTypeController(SysConfTypeService sysConfTypeService) {
        this.sysConfTypeService = sysConfTypeService;
    }

    @PostMapping(name = "新增类别信息", path = "create")
    public JsonResult create(@RequestBody ConfTypeQo confTypeQo) {
        return createOrModifyWithResult(sysConfTypeService::create, confTypeQo);
    }

    @PostMapping(name = "系统初始化信息",path = "sysInitInfo")
    public JsonResult sysInitInfo(@RequestBody Map<String,String> map){
        String sys = MapUtils.getString(map, "sys");
        Map<String, String> systemInitInfo = sysConfTypeService.getSystemInitInfo(sys);
        return DataJsonResult.succeed(systemInitInfo);
    }

    @PostMapping(name = "新增类别信息接口", path = "createApi")
    public void createApi(@RequestBody ConfTypeQo confTypeQo) {
        sysConfTypeService.create(confTypeQo);
    }

    @PostMapping(name = "更新类别信息", path = "modify")
    public JsonResult modify(@RequestBody ConfTypeQo confTypeQo) {
        return createOrModifyWithResult(sysConfTypeService::modify, confTypeQo);
    }

    @PostMapping(name = "更新类别信息接口", path = "modifyApi")
    public void modifyApi(@RequestBody ConfTypeQo confTypeQo) {
        sysConfTypeService.modify(confTypeQo);
    }

    @PostMapping(name = "删除类别信息", path = "remove")
    public JsonResult remove(@RequestBody Map<String, String> map) {
        String id = Objects.toString(map.get("id"), Strings.EMPTY);
        return removeOrOtherByIds(sysConfTypeService::remove, id);
    }

    @PostMapping(name = "更新类别信息接口", path = "removeApi")
    public void removeApi(@RequestBody ConfTypeQo confTypeQo) {
        sysConfTypeService.modify(confTypeQo);
    }

    @PostMapping(name = "物理删除类别信息接口", path = "trueRemoveApi")
    public void trueRemoveApi(@RequestBody ConfTypeQo confTypeQo) {
        sysConfTypeService.removeByRoute(confTypeQo);
    }

    @PostMapping(name = "锁定解锁接口", path = "lock")
    public JsonResult changeDelFlag(@RequestBody Map<String, String> map) {
        return createOrModifyWithResult(sysConfTypeService::lock, map);
    }

    @PostMapping(name = "分页查询类别信息", path = "search")
    public PageListJsonResult<ConfTypeQo> search(@RequestBody Map<String, String> map) {
        MultiValueMap<String, String> parameters = new LinkedMultiValueMap<>();
        if (CollUtil.isNotEmpty(map)) {
            map.forEach(parameters::add);
        }
        return queryByPaging(sysConfTypeService::queryConfTypes, new BooleanBuilder(), getPageable(map), parameters);
    }


    @PostMapping(name = "根据业务编码查询类别树", path = "tree")
    public DataJsonResult<List<ConfTypeTreeNode>> queryTree(@RequestBody Map<String, Object> map) {
        String typeCode = Objects.toString(map.get("typeCode"), Strings.EMPTY);
        boolean showHead = map.get("showHead") == null || (boolean) map.get("showHead");
        int showLevel = map.get("showLevel") == null ? -1 : (int)map.get("showLevel");
        return query(sysConfTypeService::treeByRouteCodeNotRecursion, typeCode,showHead,showLevel);
    }

    @PostMapping(name = "根据业务编码查询类别树", path = "lcodeTree")
    public DataJsonResult<List<ConfTypeTreeNode>> queryLcodeTree(@RequestBody Map<String, Object> map) {
        String typeCode = Objects.toString(map.get("typeCode"), Strings.EMPTY);
        return query(sysConfTypeService::treeByRouteCodeNotRecursionLowcode, typeCode,false,1);
    }

    @PostMapping(name = "根据业务编码查询类别树", path = "treeByLevel")
    public DataJsonResult<List<ConfTypeQo>> treeByLevel(@RequestBody Map<String, Object> map) {
        String typeCode = Objects.toString(map.get("typeCode"), Strings.EMPTY);
        int showLevel = map.get("showLevel") == null ? -1 : (int)map.get("showLevel");
        return DataJsonResult.succeed(sysConfTypeService.treeByLevel(typeCode, showLevel));
    }


    @PostMapping(name = "获取分类信息", path = "getDirClassify")
    public DataJsonResult<List<ConfTypeTreeNode>> getDirClassify() {
        return query(sysConfTypeService::treeWithRouteCode, SysConfUtil.DIR_CLASSIFY,true,-1);
    }

    @PostMapping(name = "根据业务编码查询类别树", path = "treeApi")
    public List<ConfTypeTreeNode> queryTreeApi(@RequestBody Map<String, Object> map) {
        String typeCode = Objects.toString(map.get("typeCode"), Strings.EMPTY);
        int showLevel = map.get("showLevel") == null ? -1 : (int) map.get("showLevel");
        boolean showHead = map.get("showHead") == null || (boolean) map.get("showHead");
        return sysConfTypeService.treeByRouteCodeNotRecursion(typeCode, showHead, showLevel);
    }

    @PostMapping(name = "获取分类信息", path = "getDirClassifyApi")
    public List<ConfTypeTreeNode> getDirClassifyApi() {
        return sysConfTypeService.treeWithRouteCode(SysConfUtil.DIR_CLASSIFY,true,-1);
    }

    @PostMapping(name = "获取资源主体分类", path = "getSubClassify")
    public DataJsonResult<List<ConfTypeTreeNode>> getSubClassify() {
        return query(sysConfTypeService::treeWithRouteCode, SysConfUtil.DIR_CLASSIFY_SUB,false,-1);
    }

    @PostMapping(name = "获取资源性质分类", path = "getQualClassify")
    public DataJsonResult<List<ConfTypeTreeNode>> getQualClassify() {
        return query(sysConfTypeService::treeWithRouteCode, SysConfUtil.DIR_CLASSIFY_QUAL,false,-1);
    }

    @PostMapping(name = "获取资源类目信息", path = "getCateClassify")
    public DataJsonResult<List<ConfTypeTreeNode>> getCateClassify() {
        return query(sysConfTypeService::treeWithRouteCode, SysConfUtil.DIR_CLASSIFY_CATE,true,-1);
    }

    @PostMapping(name = "获取资源类目信息", path = "getCateClassifyApi")
    public List<ConfTypeTreeNode> getCateClassifyApi() {
        return sysConfTypeService.treeWithRouteCode(SysConfUtil.DIR_CLASSIFY_CATE,true,-1);
    }

    @PostMapping(name = "根据类别编码获取节点详情", path = "getDetailByCode")
    public DataJsonResult<ConfTypeQo> getDetailByCode(@RequestBody Map<String, String> map) {
        String typeCode = Objects.toString(map.get("typeCode"), Strings.EMPTY);
        return query(sysConfTypeService::getDetailByCode, typeCode);
    }

    @PostMapping(name = "根据类别编码获取节点详情", path = "getDetailByCodeApi")
    public ConfTypeQo getDetailByCodeApi(@RequestBody Map<String, String> map) {
        String typeCode = Objects.toString(map.get("typeCode"), Strings.EMPTY);
        return sysConfTypeService.getDetailByCode(typeCode);
    }



    @PostMapping(name = "根据类别编码获取节点详情", path = "getDetailByRouteCodeApi")
    public ConfTypeQo getDetailByRouteCodeApi(@RequestBody Map<String, String> map) {
        String routeCode = Objects.toString(map.get("routeCode"), Strings.EMPTY);
        return sysConfTypeService.getDetailByRouteCode(routeCode);
    }


    @PostMapping(name = "根据父节点查询列表", path = "getListByRouteCodeApi")
    public List<ConfTypeQo> getListByRouteCodeApi(@RequestBody Map<String, String> map) {
        String routeCode = Objects.toString(map.get("routeCode"), Strings.EMPTY);
        return sysConfTypeService.getListByRouteCodeApi(routeCode);
    }

    /**
     * 废弃
     */
    @PostMapping(name = "根据业务编码查询类别树", path = "templateTree")
    public DataJsonResult<List<TreeNode>> templateTree(@RequestBody Map<String, String> map) {
        String typeCode = Objects.toString(map.get("typeCode"), Strings.EMPTY);
        return query(sysConfTypeService::templateTree, typeCode);
    }

    @PostMapping(name = "检查类别编码", path = "checkTypeCode")
    public DataJsonResult<Long> checkTypeCode(@RequestBody Map<String, String> map) {
        String id = Objects.toString(map.get("id"), Strings.EMPTY);
        String typeCode = Objects.toString(map.get("typeCode"), Strings.EMPTY);
        String parentId = Objects.toString(map.get("parentId"), Strings.EMPTY);
        return query(sysConfTypeService::checkTypeCode, id, typeCode, parentId);
    }

    @PostMapping(name = "检查类别名称", path = "checkTypeName")
    public DataJsonResult<Long> checkTypeName(@RequestBody Map<String, String> map) {
        String id = Objects.toString(map.get("id"), Strings.EMPTY);
        String typeName = Objects.toString(map.get("typeName"), Strings.EMPTY);
        String parentId = Objects.toString(map.get("parentId"), Strings.EMPTY);
        return query(sysConfTypeService::checkTypeName, id, typeName,parentId);
    }

    /**
     * 根据类别编码获取类别名称
     *
     * @param typeCode 类别编码
     * @return 类型名称
     */
    @PostMapping(path = "getNameByCode")
    public String getNameByCode(String typeCode) {
        return sysConfTypeService.getNameByCode(typeCode);
    }

    /**
     * 根据类别编码获取类别名称
     *
     * @param routeCode 类别编码
     * @return 类型名称
     */
    @PostMapping(path = "getNameByRouteCode")
    public String getNameByRouteCode(String routeCode) {
        return sysConfTypeService.getNameByRouteCode(routeCode);
    }

    /**
     * 获取树(全加载),非递归方式-返回的code是routeCode,供内部调用
     *
     * @param routeCode 路由编码，当前需要展示的节点的routeCode
     * @param showHead  是否展示头节点
     * @param showLevel 展示层级
     * @return List
     */
    @PostMapping(path = "treeWithRouteCode")
    public List<ConfTypeTreeNode> treeWithRouteCode(String routeCode, boolean showHead, int showLevel) {
        return sysConfTypeService.treeWithRouteCode(routeCode, showHead, showLevel);
    }

    @PostMapping(path = "bizMdFromDict")
    public List<KeyValue> bizMdFromDict(String code, String dictType) {
        return sysConfTypeService.bizMdFromDict(code, dictType);
    }

    @PostMapping(path = "getClassifyMapByCode")
    public Map<String, Map<String, String>> getClassifyMapByCode(@RequestBody List<String> codes) {
        return sysConfTypeService.getClassifyMapByCode(codes);
    }

    @PostMapping(path = "getCrClassify")
    public CrClassifyDto getCrClassify() {
        return sysConfTypeService.getCrClassify();
    }

    @PostMapping(path = "getResCate")
    public List<SysConfTypeDto> getResCate() {
        return sysConfTypeService.queryResourceCate();
    }

    @PostMapping(path = "getListByLikeRouteCodeApi")
    public List<ConfTypeQo> getListByLikeRouteCodeApi(String routeCode) {
        return sysConfTypeService.getListByLikeRouteCodeApi(routeCode);
    }
}
